$(function(){
	
	var taskid;
	$.ajax({
		type:"get",
		url:"activityHome/"+aid,
		/*url:"activityHome?aid="+aid,*//*传统请求方式*/
		dataType:"json",
		async:false,
		success:function(data){//回调
			console.log(data);
			endtime = data.endtime;
			//更新活动图片
			$("#banner").attr("src","img/"+data.imgurl);
			//更新活动数据
			$("#totalpeople").html(data.totalpeople);
			$("#totaltickets").html(data.totaltickets);
			$("#totalaccess").html(data.totalaccess);
			
			var now = new Date();
			if(now<data.starttime){
				$("#status").html("活动未开始");
			}else if(now>=data.endtime){
				$("#status").html("活动已结束");
			}else{
				$("#status").html("活动倒计时");
				taskid = setInterval(countdown,1000);
			}
			
			function countdown(){
				
				var now = new Date();
				if(now>=endtime){
					clearInterval(taskid);
				}else{
					//1.距离活动结束还有多长时间，活动结束时间 -当前时间（毫秒数）
					var diff = endtime - now;
					//2.根据毫秒数，算天、小时、分钟、秒
					
					var seconds = padLeft(parseInt(diff/1000%60));
					var minutes = padLeft(parseInt(diff/1000/60%60));
					var hours = padLeft(parseInt(diff/1000/60/60%24));
					var days = parseInt(diff/1000/60/60/24);
					$("#countdown").html(days+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒");
				}
				
				function padLeft(num){
					if(num<10){
						return "0"+num;
					}else{
						return num;
					}
				}
			}
		}
	});
	
	var pageNum = 1;
	
	function loadcandidate(){
		
		$.ajax({
			type:"get",
			/*url:"candidateHome/"+aid,*/
			url:"candidateHome?aid="+aid+"&pageNum="+(pageNum++),//传统请求方式
			dataType:"json",
			success:function(data){
				
				if(data.length==0){
					$("#loadmore").html("没有更多选手了");
				}else{
					
					for(var i=0;i<data.length;i++){
						var str = '<div class="c" data-id="'+data[i].cid +'">';
						str += '<img src="img/'+data[i].imgurl+'" />';
						str += '<div>'+data[i].cid+'号'+data[i].tickets + '票</div>';
						str += '<p>'+data[i].cname +'</p>';
						str += '</div>';
						
						$("#div2_2").append(str);
					}
				}
				
				if(data.length<10){
					$("#loadmore").html("没有更多选手了");
				}
			}
			
		});
		
	}
	
	loadcandidate();
	
	$("#loadmore").click(function(){
		
		loadcandidate();
	})
	
	$("#search_btn").click(function(){
		var cname = $("#search_text").val();
		if(cname==null || cname.length==0){
			alert("查询条件不能为空！");
		}else{
			$("#loadmore").css("display","none");
			$.ajax({
				type:"get",
				/*url:"candidateHome/"+aid,*/
				url:"candidateHomeByName?cname="+cname,//传统请求方式
				dataType:"json",
				success:function(data){
					//清空原有数据
					$("#div2_2").empty();
					//追加符合条件的数据
					for(var i=0;i<data.length;i++){
						var str = '<div class="c" data-id="'+data[i].cid +'">';
						str += '<img src="img/'+data[i].imgurl+'" />';
						str += '<div>'+data[i].cid+'号'+data[i].tickets + '票</div>';
						str += '<p>'+data[i].cname +'</p>';
						str += '</div>';
						
						$("#div2_2").append(str);
					}
				}
				
			});
		}
		
	})
	
	$("#btn_baoming").click(function(){
		//页面跳转
		location.href = "baoming.html?aid="+aid;
	});
	
	$(document).on("click",".c",function(){
		//1.获得cid
		var cid = $(this).attr("data-id");
		//2.页面跳转
		location.href = "details.html?aid="+aid+"&cid="+cid;
	})
	
})